package com.zhike.api.config;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import lombok.extern.slf4j.Slf4j;

@Configuration
@Slf4j
public class SaTokenConfigure implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry interceptor) {
        interceptor.addInterceptor(new HandlerInterceptor() {
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
                log.debug("token: {}", request.getHeader("satoken"));
                log.debug("请求路径: {}", request.getRequestURI());
                return true;
            }
        });
        interceptor.addInterceptor(new SaInterceptor(handler -> StpUtil.checkLogin()))
                .addPathPatterns("/**")
                // swagger start
                .excludePathPatterns("/favicon.ico")
                .excludePathPatterns("/swagger-ui/**")
                .excludePathPatterns("/swagger-resources/**")
                .excludePathPatterns("/v3/api-docs")
                .excludePathPatterns("/swagger-ui/index.html")
                .excludePathPatterns("/webjars/**")
                .excludePathPatterns("/doc.html")
                .excludePathPatterns("/error")
                // swagger end
                .excludePathPatterns("/avatar/**")
                .excludePathPatterns("/api/zkim/sys/userLogin")
                .excludePathPatterns("/api/zkim/sys/getPhoneCode")
                .excludePathPatterns("/api/zkim/sys/checkPhoneCode")
                .excludePathPatterns("/api/zkim/user/userResetPassword")
                .excludePathPatterns("/api/zkim/file/vframes")
                .excludePathPatterns("/sso/**")
                .excludePathPatterns("/api/zkim/v1/hook/**")
                .excludePathPatterns("/api/zkim/sys/selectServer")
                .excludePathPatterns("/api/zkim/user/userRegister");
    }

}
